﻿using System;
using System.Collections.Generic;
using Google.Protobuf;
using Net.Public.Msg;
using Net.Runtime.Base;

namespace Net.Public.EventSystem
{
    /// <summary>
    /// 所有客户端和服务器的消息通过事件的形式进行触发
    /// </summary>
    public class RpcEventSystem : BaseMgr<RpcEventSystem>
    {
        private Dictionary<EMsgType, Action<IMessage>> msgDic = new();

        public void RegisterMsg(EMsgType msgType, Action<IMessage> action)
        {
            if (!msgDic.TryAdd(msgType, action))
                msgDic[msgType] += action;
        }
        
        public void TriggerMsg(BaseMsg msg)
        {
            var msgType = msg.msgType;
            if(msgDic.ContainsKey(msgType))
                msgDic[msgType](msg.value);
        }
        
        public void Close()
        {
            msgDic.Clear();
            msgDic = null;
        }
    }
}